package com.cc.dao;

import com.cc.pojo.Student;
import com.cc.util.SqlHelper;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by cc on 2017/6/7.
 */
public class StudentDao {

    private static Connection ct = null;
    private static PreparedStatement ps = null;
    private static ResultSet rs = null;

    public static void add(Student student) {
        ResultSet rs = null;
        String sql = "insert into student values(?,?,?,?,?)";
        try {
            ct = SqlHelper.getConnection();
            ps = ct.prepareStatement(sql);
            ps.setString(1, student.getId());
            ps.setString(2, student.getName());
            ps.setInt(3, student.getAge());
            ps.setString(4, student.getSex());
            ps.setString(5, student.getPhoneNumber());
            ps.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            SqlHelper.close(rs, ps, ct);
        }

    }

    public static void delete(String id) {
        ResultSet rs = null;
        String sql = "DELETE FROM student WHERE id = ?";
        try {
            ct = SqlHelper.getConnection();
            ps = ct.prepareStatement(sql);
            ps.setString(1, id);
            ps.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            SqlHelper.close(rs, ps, ct);
        }
    }

    public static void update(Student student) {
        ResultSet rs = null;
        String sql = "update student set name=?,age=?,sex=?,phoneNumber=? where id=?";
        try {
            ct = SqlHelper.getConnection();
            ps = ct.prepareStatement(sql);

            ps.setString(1, student.getName());
            ps.setInt(2, student.getAge());
            ps.setString(3, student.getSex());
            ps.setString(4, student.getPhoneNumber());
            ps.setString(5, student.getId());
            ps.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            SqlHelper.close(rs, ps, ct);
        }
    }

    public static Student findById(String id) {
        ResultSet rs = null;
        String sql = "select * from student where id=" + id;
        Student student = new Student();
        PreparedStatement ps = null;

        try {
            ct = SqlHelper.getConnection();
            ps = ct.prepareStatement(sql);
            rs = ps.executeQuery();
            if (rs.next()) {
                student.setId(rs.getString(1));
                student.setName(rs.getString(2));
                student.setAge(rs.getInt(3));
                student.setSex(rs.getString(4));
                student.setPhoneNumber(rs.getString(5));
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            SqlHelper.close(rs, ps, ct);
        }
        return student;
    }


    public static List<Student> list() {
        ResultSet rs = null;
        String sql = "select * from student";
        List<Student> list = new ArrayList<Student>();
        PreparedStatement ps = null;

        try {
            ct = SqlHelper.getConnection();
            ps = ct.prepareStatement(sql);
            rs = ps.executeQuery();
            while (rs.next()) {
                Student student = new Student();
                student.setId(rs.getString(1));
                student.setName(rs.getString(2));
                student.setAge(rs.getInt(3));
                student.setSex(rs.getString(4));
                student.setPhoneNumber(rs.getString(5));
                list.add(student);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            SqlHelper.close(rs, ps, ct);
        }
        return list;
    }
    public static void main(String[] args) {
        Student student = new Student("2","sbsb",3,"male","333");
//        add(student);
//        delete("2");
//        update(student);
        Student student1 = findById("2");
        System.out.println(student1.toString());
        System.out.println(list().get(1).toString());
    }

}
